#include <stdio.h>
#include <stdlib.h>
#include <math.h>
/***************************计算三角形周长和面积*****************************/
/**
 * 点 结构体
 */
typedef struct point
{
    double x;
    double y;
} Point;

/**
 * 三角形 结构体
 */
typedef struct triangle
{
    Point *p1;
    Point *p2;
    Point *p3;
    double c; // 周长
    double s; // 面积
} Triangle;

/**
 * 释放资源
 * @param p
 */
void free_point(Point *p)
{
    free(p);
}
/**
 * 释放资源
 * @param t
 */
void free_triangle(Triangle *t)
{
    free_point(t->p1);
    free_point(t->p2);
    free_point(t->p3);
    free(t);
}
/**
 * 计算三角形面积 公式 S=(x1y2+x2y3+x3y1-x1y3-x2y1-x3y2) /2;
 * @param t
 * @return
 */
double get_s(Triangle *t)
{
    double s = t->p1->x * t->p2->y - t->p1->x * t->p3->y +
               t->p2->x * t->p3->y - t->p2->x * t->p1->y +
               t->p3->x * t->p1->y - t->p3->x * t->p2->y;
    return s / 2;
}
/**
 * 计算两点之间的距离
 * @param p1
 * @param p2
 * @return
 */
double get_d(Point *p1, Point *p2)
{

    return sqrt((p1->x - p2->x) * (p1->x - p2->x) + (p1->y - p2->y) * (p1->y - p2->y));
}
/**
 * 计算三角形周长
 * @param t
 * @return
 */
double get_c(Triangle *t)
{
    return get_d(t->p1, t->p2) + get_d(t->p2, t->p3) + get_d(t->p3, t->p1);
}
/**
 * 创建三角形
 * @param p1
 * @param p2
 * @param p3
 * @return
 */
Triangle *create_triangle(Point *p1, Point *p2, Point *p3)
{
    Triangle *pt = (Triangle *)calloc(1, sizeof(Triangle));
    pt->p1 = p1;
    pt->p2 = p2;
    pt->p3 = p3;
    pt->c = get_c(pt);
    pt->s = get_s(pt);
    return pt;
}

/**
 * 创建一个点
 * @param x
 * @param y
 * @return
 */
Point *create_point(double x, double y)
{
    Point *p = (Point *)calloc(1, sizeof(Point));
    p->x = x;
    p->y = y;
    return p;
}
int main31()
{
    double arr[] = {0, 0, 3, 0, 0, 4}; // 6个点
    Triangle *t = create_triangle(
        create_point(arr[0], arr[1]),
        create_point(arr[2], arr[3]),
        create_point(arr[4], arr[5]));
    printf("c = %f, s = %f\n", t->c, t->s); // 周长和面积
    free_triangle(t);
    return 0;
}